<?php
namespace Admin\Controller;
use Think\Controller;

class AdminlistController extends CommonController {
    public function index(){
    	$data = I('get.');
    	if(isset($data['bgtime'])){
            if($data['bgtime']==''){
                $data['btime']=strtotime('1971-01-01');
            }else{
                $data['btime']=strtotime($data['bgtime']);
            }
            if($data['endtime']==''){
                $data['etime']=time();
            }else{
                $data['etime']=strtotime($data['endtime']);
            }
    		$where['create_time'] = array('between', array($data['btime'], $data['etime']));
            $sql_all = 'where create_time between '.$data['btime'].' and '.$data['etime'];
    		
			if($data['username']!=''){
				$where['username'] = array('LIKE','%'.$data['username'].'%');
                $sql_all=$sql_all.' and username like "%'.$data['username'].'%" ';
			}
			$admins_search = M('auth_group_access')->where($where)->select();
            $this->assign('username',$data['username']);
			$this->assign('bgtime',$data['bgtime']);
			$this->assign('endtime',$data['endtime']);
			$count = count($admins_search);
            $sql = "select t1.*,t2.title from auth_group_access t1 left join auth_group t2 on t1.group_id=t2.id ".$sql_all;
            $this->page($count,$sql);
    	}else{
       		$admins_all=M('auth_group_access')->select();
            $count = count($admins_all);
            $sql = 'select t1.*,t2.title from auth_group_access t1 left join auth_group t2 on t1.group_id=t2.id ';
            $this->page($count,$sql);
    	}
    }

     public function page($count,$sql){
            $Page = new \Think\Page($count,3);// 实例化分页类 
            $sql_admin =$sql.' order by create_time limit '.$Page->firstRow.','.$Page->listRows;
            $admin = M('auth_group_access')->query($sql_admin);
            /*$admin = M('auth_group_access t1')->join('left join auth_group t2 on t1.group_id=t2.id')->select(); */
            $show = $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $this->assign('admins',$admin);
            $this->assign('count',$count);
            $this->assign('list',$list);// 赋值数据集
            $this->assign('page',$show);// 赋值分页输出
            $this->display();
     }

     public function admindel(){
        $uid = I('post.uid');

        $result = M('auth_group_access')->where(['uid'=>$uid])->delete();

        if($result){
            $this->ajaxReturn(['error'=>0,'info'=>'删除成功']);
        }else{
            $this->ajaxReturn(['error'=>1,'info'=>'删除失败']);
        }
    }

    public function stop(){
        $data = I('post.');

        if($data['status']=='0'){
            $result = M('auth_group_access')->where(['uid'=>$data['uid']])->setField('status','1');
            if($result){
                $this->ajaxReturn(['error'=>0,'info'=>'停用成功']);
            }else{
                $this->ajaxReturn(['error'=>1,'info'=>'停用失败']);
            }
        }else{
            $result = M('auth_group_access')->where(['uid'=>$data['uid']])->setField('status','0');
            if($result){
                $this->ajaxReturn(['error'=>0,'info'=>'开启成功']);
            }else{
                $this->ajaxReturn(['error'=>1,'info'=>'开启失败']);
            }
        }
    }

    public function delAll(){
        $uids = I('post.uids');

        $uids= rtrim($uids,',');
        //var_dump($aids);
        $map['uid']=array('in',$uids);
        $admins = M('auth_group_access')->where($map)->delete();

        if($admins){
            $this->ajaxReturn(['error'=>0,'info'=>'删除成功']);
        }else{
            $this->ajaxReturn(['error'=>0,'info'=>'删除失败']);
        }
    }
}

?>